# coding:utf-8
__author__ = 'admin zhoushuang'
__date__ = '2016/10/8'

from urllib import *
from base_handler import *
from common_functions import *
import os
import sys


class mod_httpfiledown(BaseHandler):
    """从http文件服务下载文件

    参数说明：
        module  =   mod_httpfiledown
        url     =   [http_url]      # http文件服务器上的目录或文件的url
        storepath   = [local_path]  # 下载文件的存储目录

    """
    def __init__(self):
        super(mod_httpfiledown, self).__init__()

    def handle(self, reqdict):
        try:
            params = extract_param(reqdict, (('url', 1), ('storepath', 1)))
        except ValueError:
            self.echo.write('#Error \trequest params error. req_dict: %s , Error: %s \n' % reqdict)
            return

        url = params.get('url')
        storepath = params.get('storepath')

        if not url.endswith('/'):
            parent = os.path.dirname(url) + '/'
            try:
                parent_list = get_http_file_list(parent, False)
            except ValueError as err:
                self.echo.write('#Error \tfailed to get parent url list: %s. error: %s \n' % (parent, err))
                return

            if url in parent_list:
                store_http_file(url, parent, storepath)
                return
            elif url+'/' in parent_list:
                url += '/'
            else:
                self.echo.write('#Error \t Not found. url :%s \n' % url)
                return

        try:
            url_list = get_http_file_list(url)
        except ValueError as err:
            self.echo.write('#Error \tfailed to get url list. url:%s .error: %s' % (url, err))
            return

        for u in url_list:
            if not u.endswith('/'):
                lpath = store_http_file(u, url, storepath)
                self.echo.write('%s ---> %s \n' % (u, lpath))


def store_http_file(file_url, replace_prefix, storepath):
    """将http服务器上的文件存到本地

    :param file_url:  文件的url地址
    :param replace_prefix:  需要被替换掉的url前缀，例如 http://.... 不能用于本地目录
    :param storepath:       本地存储的目录
    :return:                执行成功后，返回本地文件的存储路径
    """
    lpath = file_url.replace(replace_prefix, storepath+'/')
    if sys.platform[:3] == 'win':
        lpath = lpath.replace('/', '\\')
    dirpath = os.path.dirname(lpath)
    if not os.path.exists(dirpath):
        os.makedirs(dirpath)
    remfile = urlopen(file_url)
    locfile = open(lpath, 'wb')
    while True:
        dat = remfile.read(1024 * 4)
        if not dat:
            break
        locfile.write(dat)
    remfile.close()
    locfile.close()
    return lpath


def get_http_file_list(http_url_str, recursive=True):
    """获取 http 文件服务器目录下的子目录或文件的url列表

    :param http_url_str:  http url地址
    :param recursive:    是否进行递归执行，获取子目录下的列表。默认为True
    :return:             url资源列表，包括目录url和文件url
    """
    if not http_url_str.endswith('/'):
        raise ValueError('%s is not endswith / ' % http_url_str)

    rhtml = urlopen(http_url_str)
    file_list = []
    for line in rhtml.readlines():
        if line.startswith('<tr><td valign='):
            p1 = line.find('</td><td><a href=')
            p2 = line.find('</a></td><td align=')
            if p1 > 0 and p2 > 0:
                substr = line[p1:p2]
                p = substr.rfind('>')
                urlpath = http_url_str + substr[p + 1:]
                file_list.append(urlpath)
            else:
                pass
        elif line.startswith('<title>404 Not Found</title>') or line.startswith('<h1>Not Found</h1>'):
            rhtml.close()
            raise ValueError('%s is not found.' % http_url_str)

    rhtml.close()
    if recursive:
        templist = []
        for path in file_list:
            if path.endswith('/'):
                templist += get_http_file_list(path)
        file_list += templist
    return file_list


def gethandler():
    return gethandler.cache

gethandler.cache = mod_httpfiledown()


if __name__ == '__main__':

# urladd = 'http://upay:upay2016@192.168.59.185:8800/dist/product/pay-sdk/0.1.4'
# print urladd
# remotefile = urlopen(urladd)
# print 'opened'
# localfile = open('down.txt', 'wb')
#
# localfile.write(remotefile.read())
# localfile.close()
# remotefile.close()

    hand = mod_httpfiledown()
    dict = {'url': 'http://upay:upay2016@192.168.59.185:8800/dist/product/pay-sdk/0.1.4', 'storepath': "C:\\sdk\\"}
    hand.echo = sys.stdout
    hand.handle(dict)


# urladd = 'http://upay:upay2016@192.168.59.185:8800/dist/product/pay-sdk/0.1.4/'
#
# # print urladd
# #
# # remotefile = urlopen(urladd)
# # localfile = open('down.txt', 'wb')
# #
# # localfile.write(remotefile.read())
# # localfile.close()
# # remotefile.close()
#
# get_http_file_list(urladd)






